package com.gxa.service.shezhiservice.Impl;

import com.gxa.common.R;
import com.gxa.mapper.shezhimapper.KeHuMapper;
import com.gxa.pojo.KeHu;
import com.gxa.pojo.KeHuType;
import com.gxa.pojo.KeHu_Insert;
import com.gxa.service.shezhiservice.KeHuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author weishen
 */
@Service
public class KeHuServiceImpl implements KeHuService {


    @Autowired
    KeHuMapper keHuMapper;
    @Override
    public R selectAllKeHu() {
        R r = new R();
        List<KeHu> keHus = keHuMapper.selectAllKeHu();
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(keHus);
        return r;
    }

    @Override
    public int selectCountKeHu() {
        return keHuMapper.selectCountKeHu();
    }

    @Override
    public R selectAllKeHuForPage(int page, int size) {
        R r = new R();
        int index = (page - 1)*size;
        List<KeHu> keHus = keHuMapper.selectAllKeHuForPage(index,size);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setCount(keHuMapper.selectCountKeHu());
        r.setData(keHus);
        return r;
    }

    @Override
    public R selectKeHuByTypeOrBianHaoOrLianXiRenOrMingChenOrPhone(int page, int size,
                                                                   String keHuType, String keHuBianHao,
                                                                   String keHuMingCheng, String lianXiRen,
                                                                   String phone) {
        R r = new R();
        int index = (page - 1)*size;
        List<KeHu> keHus = keHuMapper.selectKeHuByTypeOrBianHaoOrLianXiRenOrMingChenOrPhone(index,size,keHuType,
                keHuBianHao,keHuMingCheng,lianXiRen,phone);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setCount(selectCountAnyKeHu(keHuType,
                keHuBianHao,keHuMingCheng,lianXiRen,phone));
        r.setData(keHus);
        return r;
    }

    @Override
    public int selectCountAnyKeHu(String keHuType, String keHuBianHao, String keHuMingCheng, String lianXiRen, String phone) {
        return keHuMapper.selectCountAnyKeHu(keHuType,
                keHuBianHao,keHuMingCheng,lianXiRen,phone);
    }

    @Override
    public R insertKeHu(KeHu_Insert keHu) {
        R r= new R();
        int res = keHuMapper.insertKeHu(keHu);
        r.setCode(500);
        r.setMsg("新增失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("新增成功");
        }
        return r;
    }

    @Override
    public R updateKeHuById(KeHu keHu) {
        R r = new R();
        int res = keHuMapper.updateKeHuById(keHu);
        r.setCode(500);
        r.setMsg("更新失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("更新成功");
        }
        return r;
    }

    @Override
    public R deleteKeHuById(ArrayList<Integer> id) {
        R r = new R();
        int res=0;
        int flag=1;
        for(int i=0;i<id.size();i++)
        {
            res=keHuMapper.deleteKeHuById(id.get(i));
            if(res==0)
            {
                flag=0;
                break;
            }
        }
        r.setCode(200);
        r.setMsg("删除成功");
        if(flag==0)
        {
            r.setCode(500);
            r.setMsg("删除失败");
        }
        return r;
    }

    @Override
    public R selecAllKeHuType() {
        R r =new R();
        List<KeHuType> list = keHuMapper.selecAllKeHuType();
        r.setCode(200);
        r.setData(list);
        r.setMsg("查询成功");
        return  r;
    }



}
